package com.guet.main.Listener;

/**
 * @Classname SubjectExcelListener
 * @Description done
 * @Date 2022/7/29 17:01
 * @Created by csp
 */

import cn.hutool.crypto.SecureUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.guet.main.entity.User;
import com.guet.main.entity.vo.UserVo;
import com.guet.main.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
public class UserListener extends AnalysisEventListener<UserVo> {

    public UserService userService;

    public UserListener(UserService userService) {
        this.userService = userService;
    }

    public UserListener() {
    }

    //读取excel内容，一行一行读取
    @Override
    public void invoke(UserVo userVo, AnalysisContext analysisContext) {

        User user = new User();
        BeanUtils.copyProperties(userVo,user);
        user.setPassword(SecureUtil.md5(userVo.getAccount()));
        userService.save(user);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
